var express = require('express');
var router = express.Router();

var { insertMenu, selectMenu } = require("../db/menu.js")

/* GET users listing. */
router.get('/', function (req, res, next) {
	res.send('respond with a resource');
});

// 新增菜单
router.post('/addMenu', function (req, res, next) {
	let body = req.body;
	let data = {}
	insertMenu(body).then(e => {
		console.log(e)
		if (e) {
			data.message = "保存成功"
			data.code = "0"
		} else {
			data.message = "保存失败"
			data.code = "1"
		}
		res.send(data)
	}).catch(e => {
		data.message = "系统出错"
		data.code = "err"
		data.data = e
		res.send(data)
	})
})

// 获取菜单
router.post('/getMenus', function (req, res, next) {
	let data = {}
	selectMenu().then(e => {
		if (e) {
			data.message = "获取成功"
			data.code = "0"
			data.data = e
		} else {
			data.message = "获取失败"
			data.code = "1"
		}
		res.send(data)
	}).catch(e => {
		data.message = "系统出错"
		data.code = "err"
		data.data = e
		res.send(data)
	})
})

module.exports = router;
